#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: solution.py 
@version:
@time: 2024/01/21 
"""


def split(params_str):
    p = []
    start = 0
    end = params_str.find(' ')
    while end != -1:
        p.append(int(params_str[start:end]))
        start = end + 1
        end = params_str.find(' ', start)
    p.append(int(params_str[start:]))
    return p


def split_str(params_str):
    p = []
    start = 0
    end = params_str.find(' ')
    while end != -1:
        p.append(params_str[start:end])
        start = end + 1
        end = params_str.find(' ', start)
    p.append(params_str[start:])
    return p


n = int(input())
tree = {}
for _ in range(n):
    input_temp = input()
    temp = split_str(input_temp)
    a = temp[0]
    b = temp[1]
    if b not in tree:
        tree[b] = []
    tree[b].append(a)

target = input()

childrens = tree.get(target, [])
result = []

while childrens:
    node = childrens[0]
    childrens = childrens[1:]
    result.append(node)

    if node in tree:
        childrens.extend(tree[node])

result.sort()
for res in result:
    print(res)
"""
input

```text
5
b a
c a
d c
e c
f d
c
```

ouput

```text
d
e
f
```
"""